<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/base.css?{:time()}" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/indexbody.css?{:time()}" />
<script type="text/javascript" src="__PUBLIC__/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/bootstrap.min.js"></script>
<link href="http://www.cditv.cn/statics/css/reset.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://appwuhan.cn:8081/statics/js/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
<link href="http://appwuhan.cn:8081/statics/js/jquery.easyUI/themes/gray/easyui.css" rel="stylesheet" type="text/css" />
<link href="http://appwuhan.cn:8081/statics/js/jquery.easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
<link href="http://appwuhan.cn:8081/statics/js/artDialog-5.0.4/skins/blue.css" rel="stylesheet" type="text/css" />
<script src="http://appwuhan.cn:8081/statics/js/jquery.easyUI/jquery.easyui.js" type="text/javascript"></script>


    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">工作流设置</h4>
        </div>
        <form action="" method="POST" class="form-horizontal" style="padding:15px 0">
        
         <div id="toolbar">
            <a href="javascript:addStep();" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加审核流</a>
        </div>

        <ul id="tt"></ul>

        <div id="dd" class="easyui-dialog" title="My Dialog" style="width:400px;height:200px;" data-options="iconCls:'icon-save',resizable:true,modal:true" closed="true">  
            <div style="padding:10px 60px 20px 60px">
                <table cellpadding="5">
<!--                <tr> -->
                    <!-- <input class="easyui-textbox" type="text" name="link_title" data-options="required:true"></input> -->
<!--                </tr>   -->

                <tr>
                    <td><input id="cc" value=""></td>
                </tr>
                </table>    
            </div>
        </div>


        <div id="stepmenu" class="easyui-menu" style="width:120px;">
                <div onclick="appendStep('before')" data-options="iconCls:'icon-add'">前插入审核流</div>
                <div onclick="appendStep('after')" data-options="iconCls:'icon-add'">后插入审核流</div>
                <div onclick="addUser()" data-options="iconCls:'icon-add'">添加人员</div>
                <div onclick="removeStep()" daqueryParamsta-options="iconCls:'icon-remove'">删除审核流</div>
        </div>


        <div id="usermenu" class="easyui-menu" style="width:120px;">
                <div onclick="addUser()" data-options="iconCls:'icon-add'">添加人员</div>
                <div onclick="removeUser()" data-options="iconCls:'icon-remove'">删除人员</div>
        </div>



        <div class="add-error-message error item"></div>

        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-10">
              <input type="hidden" name="chid" value="0" />
              <button type="button" class="btn btn-success" onclick="javascript:saveWorkflow();" id="editchannelBtn">确定修改</button>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <button type="button" class="btn btn-danger"  data-dismiss="modal">取消</button>
            </div>
          </div>
        </form>
    </div>


<script type="text/javascript">

var treedata = <?= json_encode($res) ?>;
var querytimer;


$(document).ready(function(){

    $('#tt').tree({
        data:treedata,
    });

    
    $('#tt').tree({
        onContextMenu: function(e, node){
            e.preventDefault();
            
            $('#tt').tree('select', node.target);
            if($("#tt").tree("getParent",node.target)!=null){
                    //用户菜单
                    
                $('#usermenu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
                
                }else{
                    //审核流菜单
                    
                    
                    $('#stepmenu').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                    

                    }
            }
    });     


    
    $('#cc').combobox({
      url: '{:U("Admin/System/ajaxUserList")}',
      hasDownArrow:false,
        formatter:function(node){
            return node.realname;
        },
        onSelect: function(node){
            
            var top=getTopStep();
            $('#tt').tree('append', {
                parent: top.target,
                data: [{
                    text:node.realname,
                    uid:node.uid
                }]
            });
            
            
        },
        keyHandler: {
            query: function(q,e){
                clearTimeout(querytimer);
                querytimer=setTimeout(function(){ 
                    $.get("{:U("Admin/System/ajaxUserList")}",{search_real_name:q},function(json){
                    var qvalue= $('#cc').combobox('getText');
                    console.log(qvalue);
                    $('#cc').combobox('loadData',json);
                    $('#cc').combobox('setText',qvalue);            
                 },"json");
                     }, 1000);
                },
        }
    });

    
});



function removeStep(){
    var selected = $('#tt').tree('getSelected');
    if (selected){
        $.messager.confirm('Confirm','你确定要执行删除操作？',function(r){
     if (r){
                 $('#tt').tree('remove',selected.target);
                      redata();
            }
        });
    }
}


function redata(){
    var roots=$("#tt").tree("getRoots");
    for(var i=0;i<roots.length;i++){
        node=roots[i];
        $('#tt').tree('update', {
            target: node.target,
            text: (i+1)+"级审核"
        });
    }
}


function getTopStep(){
     var selected = $('#tt').tree('getSelected');
     var parent=$("#tt").tree("getParent",selected.target);
     if(parent!=null){
          //父亲
         return parent;
      }else{
             //自己
            return selected;
       }
}



function addStep(){
    $('#tt').tree('insert', {
        data: {
            id:'',
            text: ''
        }
    });
    redata();
}




function appendStep(type){
    var node = $('#tt').tree('getSelected');

    if(type=="before"){

        $('#tt').tree('insert', {
            before: node.target,
            data: {
                id:'',
                text: ''
            }
        });
        
    }else if(type=="after"){
        
        $('#tt').tree('insert', {
            after: node.target,
            data: {
                id:'',
                text: ''
            }
        });

    }
    

    redata();
}


function addUser(){
    $('#dd').dialog('open').dialog('setTitle','添加人员');
}



function removeUser(){
    var selected = $('#tt').tree('getSelected');
    if (selected){
         $('#tt').tree('remove',selected.target);
    }
}



function  saveWorkflow(){

    var roots=$("#tt").tree("getRoots");
    var node_val='';
    var node='';
    
    if(roots.length>0){

        for(var i=0;i<roots.length;i++){
            
            node+="&node[]="+(i+1);
            
            var childrens=$("#tt").tree("getChildren",roots[i].target);

            
            if(childrens==null || childrens.length==0){
                alert("请先删除空的审核节点");
                return;
            }
            
            for(var index in childrens ){
                node_val+="&node_val_"+(i+1)+"[]="+childrens[index].uid;
            }
        }
            
        
        }

     $.post('{:U('Admin/System/workFlowEdit')}',node+node_val+'&'+'chid={$chid}',function(json){
        alert(json.msg);
    },'json');  
}

</script>
